import axios from 'axios';

// eslint-disable-next-line no-undef
var backendUrl = loginUrl;

export const setUrl = () => {
	// eslint-disable-next-line no-undef
	backendUrl = loginUrl;
};

/**
 * 坐席登录
 * @param {*} name
 * @param {*} pwd
 * @param {*} org
 */
export const login = (name, pwd, org) => {
	// eslint-disable-next-line no-undef
	return axios.post(backendUrl + '/ivcs/api/v1/auth/logon', {
		userName: name,
		pwd: pwd,
		role: 'agent',
		org: org
	});
};

/**
 * 获取会话的所有记录
 * @param {*} sessionId
 */
export const getsessiondetail = sessionId => {
	return axios.get(backendUrl + '/ivcs/api/v1/sessions/' + sessionId + '/record');
};

/**
 * ocr图片识别
 * @param {*} picUrl  图片地址
 * @param {*} side   正反面
 */
export const ocrPicture = (picUrl, side) => {
	return axios.post(backendUrl + `/ivcs/api/v1/ocr/idcard`, {
		idcardUrl: picUrl,
		side: side
	});
};

/**
 * 获取访客提交的form信息
 * @param {*} formid
 */
export const getuserforminfo = formid => {
	return axios.get(backendUrl + '/ivcs/api/v1/formInfo/' + formid);
};

/**
 * 获取访客提交的个人信息
 * @param {*} sessionId
 */
export const getsessionformdata = sessionId => {
	return axios.get(backendUrl + '/ivcs/api/v1/sessions/' + sessionId + '/applications');
};

/**
 * 获取推送视频链接的数据，返回数组
 */
export const getVideoLinkResource = () => {
	return axios.get(backendUrl + '/ivcs/api/v1/public/resource-link');
};

/**
 * 设置登录后的返回值
 * @param {*} result
 * @param {*} resp
 */
export const storageLoginData = (result, resp) => {
	localStorage.setItem('agentName', result.data.userName);
	localStorage.setItem('agentToken', resp.data.data.accessToken);
	localStorage.setItem('userId', result.data.id);
	localStorage.setItem('agentOrg', result.data.org);
	var urls = resp.data.data.urls;
	localStorage.setItem('serverUrl', urls.backendurl);
	localStorage.setItem('amsUrl', urls.amsurl);
	localStorage.setItem('uploadUrl', urls.uploadUrl + '/v1/upload');
	localStorage.setItem('turnserverUrl', urls.turnserverUrl);
	localStorage.setItem('turnserverCredential', urls.turnserverCredential);
	localStorage.setItem('turnserverUsername', urls.turnserverUsername);
	localStorage.setItem('rtmpServer', urls.rtmpServer);
};

/**
 * 缓存登录信息
 * @param {*} resp
 * @param {*} groupNames
 */
export const cacheLoginData = (resp, groupNames) => {
	sessionStorage.setItem('groupsNames', groupNames);
	localStorage.setItem('serverUrl', resp.backendUrl);
	// eslint-disable-next-line no-undef
	backendUrl = loginUrl;
};

/**
 * 上传文件
 * @param {*} data
 */
export const uploadFileWithFormData = data => {
	return axios.post(backendUrl + '/ivcs/api/v1/upload', data, {
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	});
};

/**
 * 获取所有启用的业务场景
 */
export const getAllBizScenario = () => {
	// status = enabled 表示所有启用的业务场景  disabled 表示所有停用的业务场景
	return axios.get(backendUrl + '/ivcs/api/v1/bizscenarios/select/options?status=enabled');
};

/**
 * 保存业务流程的完成情况
 * @param para
 * @returns {Promise<AxiosResponse<T>>}
 */
export const saveWorkFlowStep = para => {
	return axios.post(backendUrl + `/ivcs/api/v1/humanFlow/step`, para);
};

/**
 * 根据返回值给不同的提示语
 * @param {*} response
 * @param {*} responseErrorMessage
 */
export const responseError = (response, responseErrorMessage) => {
	if (response && response.data) {
		var code = response.data.code;
		if (code === '1302') {
			responseErrorMessage('ivcs.pwdSame');
		} else if (code === '1001') {
			responseErrorMessage('ivcs.userNotExist');
		} else if (code === '1002') {
			responseErrorMessage('ivcs.userExist');
		} else if (code === '1103') {
			responseErrorMessage('ivcs.pwdError');
		} else if (code === '1104') {
			responseErrorMessage('ivcs.accountError');
		} else if (code === '1401') {
			responseErrorMessage('ivcs.dbError');
		} else if (code === '1301') {
			responseErrorMessage('ivcs.sendDataError');
		} else if (code === '1101') {
			responseErrorMessage('ivcs.accountAuthError');
		} else if (code === '1102') {
			responseErrorMessage('ivcs.requestReject');
		} else if (response.status === 401) {
			responseErrorMessage('ivcs.accountInvalid');
		}
	} else {
		responseErrorMessage('undefined');
	}
};

/**
 * 根据url返回blob数据
 * @param {*} url
 */
export const getImageByUrl = url => {
	return axios.get(url, {
		headers: {},
		responseType: 'blob'
	});
};

/**
 * 人脸识别
 * @param userId 用户id
 * @param para 图片参数
 * @returns {Promise<AxiosResponse<T>>}
 */
export const compareFace = (userId, para) => {
	return axios.post(backendUrl + `/ivcs/api/v1/users/` + userId + '/compare-face', para);
};

/**
 * 上传图片根据blob
 * @param blob
 * @param fileName
 * @returns {Promise<AxiosResponse<T>>}
 */
export const uploadFileWithBlob = (blob, fileName) => {
	var fd = new FormData();
	fd.append('file', blob, fileName);
	return axios.post(backendUrl + '/ivcs/api/v1/upload', fd, {
		headers: {
			'Content-Type': 'multipart/form-data'
		}
	});
};
// 判断当前用户是否是当前分组的班组长坐席

export const judgeChief = (userId, groupId) => {
	return axios.get(backendUrl + '/ivcs/api/v1/user-group-relations/isChief?userId=' + userId + '&groupId=' + groupId);
};

// 分页获取指定分组下相关坐席
export const findAgents = info => {
	return axios.post(backendUrl + '/ivcs/api/v1/agents/find', info);
};

/**
 * 获取roomtoken，用于通话管理中心，静默观战
 * @param {string} roomid
 * @param {string} userName
 * @param {string} token
 */
export const getRoomToken = (roomid, userName) => {
	return axios.post(backendUrl + '/ivcs/api/v1/tokens', {
		room: roomid,
		user: userName ? userName : 'admin',
		role: 'presenter'
	});
};

// 标记已完成人工工作流
/**
 *
 * @param { Object } data sessionId 会话id,  stepId: 工作流步骤 id
 * @returns
 */
export const finishedWorkFlow = data => {
	return axios.post(`${backendUrl}/ivcs/api/v1/humanFlow/step/finish`, data);
};

/**
 * 获取session详情，获取业务场景下的人工工作流
 * @param {Object} data sessionId 会话id groupId 分组id
 * @param {String} token token
 * @returns {Promise<AxiosResponse<T>>}
 */
export const getHumanFlowDetailWithSessionId = data => {
	return axios.get(`${backendUrl}/ivcs/api/v1/humanFlow/detail/sessionId/${data.sessionId}/scenarioId/${data.scenarioId}`);
};

/**
 * 根据坐席工号获取绑定的业务场景
 * @param {String} username 坐席工号
 * @param {String} token token
 */
export const getAgentBindBizScenario = username => {
	return axios.get(`${backendUrl}/ivcs/api/v1/bizscenarios/agent/${username}`);
};

/**
 * 根据坐席ID获取表单列表
 * @param {Number} page 页面号
 * @param {Number} pageSize 页面数
 * @param {String} sessionId 会话Id
 * @param {String} followId 表单流水号
 * @param {String} customerName 访客名称
 * @param {Date} pushStartTime 推送开始时间
 * @param {Date} pushEndTime 推送结束时间
 * @param {Date} submitStartTime 提交开始时间
 * @param {Date} submitEndTime 提交结束时间
 * @param {String} name 表单名称
 * @param {String} status 表单状态
 */
export const getFormDataList = data => {
	return axios.get(
		`${backendUrl}/ivcs/api/v1/formHistory/list?page=${data.page}&pageSize=${data.pageSize}&sessionId=${data.sessionId}&followId=${data.followId}&customerName=${data.customerName}&fromClientId=${data.fromClientId}&pushStartTime=${data.pushStartTime}&pushEndTime=${data.pushEndTime}&submitStartTime=${data.submitStartTime}&submitEndTime=${data.submitEndTime}&name=${data.name}&status=${data.status}`
	);
};

/**
 *
 * @param {String} scenId 业务场景id
 * @param {String} token token
 */
export const getFormNameByScenId = scenId => {
	return axios.get(`${backendUrl}/ivcs/api/v1/bizscenarios/${scenId}/forms`);
};

/**
 *生成表单流水号
 * @param {Object} data org:机构 sessionId:会话id toClientId:访客端id fromClientId:坐席端id templateId:表单id
 * @param {String} token token
 */
export const getFormFollowId = data => {
	return axios.post(`${backendUrl}/ivcs/api/v1/formHistory`, data);
};

/**
 * 更新表单记录
 * @param {String} data id:表单历史id status:表单状态 success fail timeout data: JSON 数据
 * @param {String} token
 */
export const updateFormRecord = data => {
	return axios.put(`${backendUrl}/ivcs/api/v1/formHistory/${data.id}`, data);
};

/**
 * 获取所有表单名称
 * @returns
 */
export const getAllFormName = () => {
	return axios.get(`${backendUrl}/ivcs/api/v1/formTemplates/all`);
};

/**
 *获取表单详情
 * @param { Number } id 表单记录 ID
 * @returns
 */
export const getFormDetail = id => {
	return axios.get(`${backendUrl}/ivcs/api/v1/formHistory/${id}`);
};

/**
 * 根据流ID 移除数字人流
 * @param { String } room  房间号
 * @param { String } streamId 流 ID
 * @param { String } token
 * @returns
 */
export const dropRobotStream = (room, streamId) => {
	return axios.delete(`${backendUrl}/ivcs/api/v1/rooms/${room}/streams/${streamId}`);
};

/**
 *更新用户定位信息
 * @param {String} sessionId 会话ID
 * @param {String} locationAddress 定位地址
 * @param {String} token 会话Token
 */
export const sumbitLocation = (sessionId, locationAddress) => {
	return axios.put(`${backendUrl}/ivcs/api/v1/sessions/${sessionId}/formInfo/locationAddress`, { locationAddress });
};

/**
 * 获取会话中在线的坐席及业务场景
 * @param {String} sessionId 会话id
 */
export const getAgentsWithSession = sessionId => {
	return axios.get(`${backendUrl}/ivcs/api/v1/sessions/${sessionId}/agents/online`);
};

/**
 *设置会话人工流程的状态
 * @param {String} sessionId  会话id
 * @param {Number} scenarioId 业务场景id
 * @param {String} status 状态 complete-完成 incomplete-未完成
 */
export const setHumanFlowStatus = (sessionId, scenarioId, status) => {
	return axios.put(`${backendUrl}/ivcs/api/v1/humanFlow/detail/sessionId/${sessionId}/scenarioId/${scenarioId}/status`, { status });
};
